﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace B10.Helpers { 

    public class PaginatedList<T> : List<T> {

        public PaginatedListInfo ListInfo { get; private set; }

        public PaginatedList(IQueryable<T> source, int pageIndex, int pageSize) {
            ListInfo = new PaginatedListInfo(source.Count(),pageIndex,pageSize);

            this.AddRange(source.Skip((ListInfo.PageIndex - 1) * ListInfo.PageSize)
                .Take(ListInfo.PageSize));
        }

        
    }

    public class PaginatedListInfo
    {
        public PaginatedListInfo(int count, int pageIndex, int pageSize)
        {
            PageIndex = pageIndex;
            PageSize = pageSize;
            TotalCount = count;
            TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);
        }

        public int PageIndex { get; private set; }
        public int PageSize { get; private set; }
        public int TotalCount { get; private set; }
        public int TotalPages { get; private set; }

        public bool HasPreviousPage
        {
            get
            {
                return (PageIndex > 1);
            }
        }

        public bool HasNextPage
        {
            get
            {
                return (PageIndex < TotalPages);
            }
        }
    }
}
